package com.ruoyi.stock2.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TextToPinYin {
    private static final Logger log = LoggerFactory.getLogger(TextToPinYin.class);
	public static String toPinYin(String text) {

		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        // format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        StringBuilder sb = new StringBuilder();
        char[] chars = text.toCharArray();
        for (char aChar : chars) {

            if (Character.isWhitespace(aChar)) {
                continue;
            }
            if (aChar >= -128 && aChar <= 127) {
                sb.append(aChar);
            } else {
                String s = null;
                try {
                    s = PinyinHelper.toHanyuPinyinStringArray(aChar, format)[0];
                    sb.append(s.charAt(0));
                } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                    badHanyuPinyinOutputFormatCombination.printStackTrace();
                }
            }
        }
        log.warn(text + " to " + sb.toString());
        return sb.toString();
    }
	
	
}
